package com.woniuxy.mesboot.controller;

import com.woniuxy.mesboot.entity.MesProcess;
import com.woniuxy.mesboot.service.ProcessDetailsService;
import com.woniuxy.mesboot.service.ProcessService;
import com.woniuxy.mesboot.service.productProcessService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.vo.MesProcessVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("process")
public class ProcessController {
    @Resource
    private ProcessService processService;

    @Resource
    private ProcessDetailsService processDetailsService;

    @Resource
    private productProcessService productProcessService;

    @GetMapping("/select")
    public Result select() {
        List<MesProcessVO> mesProcessVOS = processService.selectProcedureList();
        return Result.ok().data("list",mesProcessVOS);
    }

    @GetMapping
    public Result queryAll() {
        List<MesProcess> i = processService.selectAll();
        return Result.ok().data("list", i);

    }

    @GetMapping("queryProcedures")
    public Result queryProcedureList() {

        return Result.ok();
    }

//    @PostMapping
//    public Result insert(@RequestBody ProcessDTO record) {
//        MesProcess mesProcess = new MesProcess();
//        mesProcess.setOperationProcedureId(record.getOpreationProcedureId());
//        mesProcess.setOperationProcedureContent(record.getOperationProcedureContent());
//        mesProcess.setWorkNo(record.getWorkNo());
//        processService.insertSelective(mesProcess);
//        ProductProcess productProcess = new ProductProcess();
//        productProcess.setProductId(record.getProductId());
//        productProcess.setOperationProcedureId(record.getOpreationProcedureId());
//        productProcessService.insertSelective(productProcess);
//        final List<MesProcessDetails> mesProcessDetailsList = record.getMesProcessDetailsList();
//        MesProcessDetails mesProcessDetails = new MesProcessDetails();
//        for (int i = 0; i < mesProcessDetailsList.size(); i++) {
//            mesProcessDetails = mesProcessDetailsList.get(i);
//            processDetailsService.insertSelective(mesProcessDetails);
//        }
//        return Result.ok();
//    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id) {
        int i = processService.deleteByPrimaryKey(id);
        if (i > 0) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("{id}")
    public Result queryById(@PathVariable Long id) {
        MesProcess mesProcess = processService.selectByPrimaryKey(id);
        return Result.ok().data("item", mesProcess);
    }

    @PutMapping
    public Result update(@RequestBody MesProcess record) {
        int i = processService.updateByPrimaryKey(record);
        if (i > 0) {
            return Result.ok("修改成功");

        } else {
            return Result.error("修改失败");
        }
    }
}
